package com.kfm.mvn.listener;

import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class MyServletContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext();

        // 动态注册过滤器
        FilterRegistration.Dynamic myFilter = servletContext.addFilter("MyFilter", "com.kfm.filter.MyFilter");
        myFilter.addMappingForUrlPatterns(null, false, "/hello");

        System.out.println("MyServletContextListener.contextInitialized");

    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("MyServletContextListener.contextDestroyed");
    }
}
